<template>
  <div
    class="seckill-manage-tab1"
    v-loading="loading"
    element-loading-text="加载中..."
  >
    <div class="seckill-manage-title">
      <div class="seckill-manage-title-left">
        <el-form inline>
          <el-form-item>
            <el-input
              v-model="searchForm.name"
              placeholder="商品名称"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="searchFun">查询</el-button>
            <el-button @click="resetFun">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="seckill-manage-title-right">
        <el-button type="success" :icon="Back" @click="goBack">
          返回
        </el-button>
        <el-button type="primary" :icon="Plus" @click="addDeptFun('add')">
          添加
        </el-button>
      </div>
    </div>
    <div class="seckill-manage-table">
      <el-table
        @selection-change="handleSelectionChange"
        border
        stripe
        :data="tableData"
        :height="store.tableHeight - 40"
      >
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column prop="goods_name" label="商品名称" width="500">
          <template #default="{ row }">
            <div class="manage-info">
              <el-image
                :preview-teleported="true"
                style="width: 50px; min-width: 50px; height: 50px"
                :src="imgSrc(row.goods?.cover)"
                :zoom-rate="1.2"
                :preview-src-list="[imgSrc(row.goods?.cover)]"
                :initial-index="4"
                fit="cover"
                class="img-box"
              />
              <div class="right">
                <div>
                  <label>商品名称：</label>
                  <span>{{ row.goods_name }} </span>
                </div>
                <div>
                  <label>商品编号：</label>
                  <span>{{ row.goods_code }} </span>
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="stock" label="秒杀库存"></el-table-column>
        <el-table-column label="已售" prop="stock_sale"></el-table-column>
        <el-table-column label="活动状态" width="100">
          <template #default="{ row }">
            <el-tag v-if="row.status === 0">未开始</el-tag>
            <el-tag v-else-if="row.status === 1" type="success">进行中</el-tag>
            <el-tag v-else-if="row.status === 2" type="danger">已结束</el-tag>
            <el-tag v-else-if="row.status === -1" type="info">已关闭</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" align="center">
          <template #default="{ row }">
            <el-button type="primary" link @click="updateDeptFun(row)">
              查看
            </el-button>
            <el-button type="danger" link @click="delFun(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="seckill-manage-table-foot" style="margin-top: 10px">
        <div></div>
        <el-pagination
          v-model:currentPage="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[20, 50, 100, 200]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
        <div></div>
      </div>
    </div>
  </div>
  <addDialog ref="addDialogRef" @refreshList="refreshList" />
  <skuPage ref="skuPageRef" />
</template>
<script setup>
import { Plus, Back } from "@element-plus/icons-vue";
import { useCounterStore } from "@/stores/counter.js";
import { listApi, delApi } from "@/plugins/api/shopping/seckillManage";
import addDialog from "./components/DialogSeckill-shop.vue";
import skuPage from "./components/DialogSeckill-shop-sku.vue";
const store = useCounterStore();
const router = useRouter();
const route = useRoute();
let tableData = ref([]);

let loading = ref(false);
let searchForm = ref({});
function searchFun() {
  currentPage.value = 1;
  selectData.value = [];
  tableData.value = [];
  getData();
}
function resetFun() {
  currentPage.value = 1;
  searchForm.value = {};
  tableData.value = [];
  selectData.value = [];
  getData();
}
const currentPage = ref(1);
const pageSize = ref(20);
const total = ref(0);
const handleSizeChange = (val) => {
  pageSize.value = val;
  selectData.value = [];
  getData();
};
const handleCurrentChange = (val) => {
  currentPage.value = val;
  selectData.value = [];
  getData();
};
let goodsId = ref(null);
provide("seckillGoodsId", goodsId);
function getData() {
  loading.value = true;
  let obj = Object.assign(
    {
      seckill_id: goodsId.value,
      page: currentPage.value,
      pageSize: pageSize.value,
    },
    searchForm.value
  );
  listApi(obj)
    .then((data) => {
      tableData.value = data.list;
      total.value = data.page.total;
      loading.value = false;
    })
    .catch(() => {
      loading.value = false;
    });
}

let selectData = ref([]);
function handleSelectionChange(val) {
  selectData.value = val;
}
// 新增秒杀商品
let addDialogRef = ref(null);
function addDeptFun(type) {
  addDialogRef.value.initFun(type);
}
// 查看sku
let skuPageRef = ref(null);
function updateDeptFun(row) {
  console.log(JSON.parse(row.data_items));
  skuPageRef.value.initFun("look", row);
}
//
function refreshList() {
  getData();
}
function statusFun(row) {
  stateApi({
    status: row.status,
    id: row.id.toString(),
  }).then(() => {
    ElMessage({
      type: "success",
      message: "修改成功！",
    });
    getData();
  });
}
function delFun(row) {
  ElMessageBox.confirm("确认删除，是否继续?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delApi({
        id: row.id.toString(),
      }).then(() => {
        ElMessage({
          type: "success",
          message: "删除成功！",
        });
        getData();
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消。",
      });
    });
}

function imgSrc(path) {
  return import.meta.env.VITE_APP_BASE_API + path;
}
function initFun() {
  currentPage.value = 1;
  getData();
}
defineExpose({
  initFun,
});
// 返回
function goBack() {
  router.back();
}
onMounted(() => {
  if (route.query.manageId) {
    goodsId.value = route.query.manageId;
    getData();
  } else {
    router.back();
  }
});
</script>
<style scoped lang="scss">
.seckill-manage-tab1 {
  width: 100%;
  height: 100%;
  padding: 10px;
  box-sizing: border-box;

  .seckill-manage-title {
    width: 100%;
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #eeeeee;
    padding-bottom: 10px;

    .seckill-manage-title-left {
      font-size: 18px;
      font-weight: 700;
      color: #172b4d;

      :deep(.el-form-item) {
        margin-bottom: 0 !important;
      }
    }
  }

  .seckill-manage-table {
    box-sizing: border-box;

    .manage-info {
      display: flex;

      .img-box {
        align-self: center;
      }

      .right {
        margin-left: 10px;
        align-self: center;

        span {
          color: #409eff;
        }
      }
    }

    .seckill-manage-table-foot {
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
